package com.zs4sdk.activity.login;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.common.library.data.CommonEventHandler;
import com.common.library.data.EventHandler2Get;
import com.common.library.util.ActivityHelper;
import com.common.library.util.EncryptHelper;
import com.common.library.util.HandlerUtil;
import com.common.library.util.ParamsHelper;
import com.zs4gamesdk.R;
import com.zs4sdk.common.Extra;
import com.zs4sdk.dialog.Zs4GameTipDialog;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/5/25.
 */
//繁体帐号登录
public class Zs4GameLoginAccountFt extends Zs4GameLoginBase {
    private ViewStub stuAccountRegist;
    private ViewStub stuAccountLogin;
    private ViewStub stuOtherAccountLogin;
    private Button btnLogin;
    private TextView tvTip;
    private TextView tvSwitch;
    private final String goToRegistText;
    private final String goToLoginText;
    private final String loginText;
    private final String loginAndRegistText;
    private static final int LOGIN = 1;
    private static final int REGIST = 2;
    private int loginStyle = LOGIN;
    private boolean isInitAccountRegist = false;
    private EditText etAccount ;
    private EditText etPassword;
    private EditText etRegistAccount;
    private EditText etRegistPassword;
    private MyZs4GameTipDialog myZs4GameTipDialog;
    public Zs4GameLoginAccountFt(Activity act, View view, Handler handler) {
        super(act, view, handler);
        goToRegistText = act.getResources().getString(R.string.zs4_game_no_account_to_regist);
        goToLoginText = act.getResources().getString(R.string.zs4_game_go_to_login);
        loginAndRegistText = act.getResources().getString(R.string.zs4gamestring_btn_register_and_login);
        loginText = act.getResources().getString(R.string.zs4gamestring_btn_login);
        stuOtherAccountLogin = (ViewStub)view.findViewById(R.id.stu_lib_other_account_login);

    }

    @Override
    public void login() {
        if(loginStyle == LOGIN){
            accountLogin();
        }else if(loginStyle == REGIST){
            if(myZs4GameTipDialog == null){
                myZs4GameTipDialog = new MyZs4GameTipDialog(mActivity);

            }
            myZs4GameTipDialog.showDialog();
            //accountRegist();
        }
    }

    private void accountLogin(){
        String account = etAccount.getText().toString();
        if(account.length() == 0){
            ActivityHelper.showLongToast(mActivity,mActivity.getString(R.string.zs4Game_account_no_empty));
            return;
        }
        String passowrd = etPassword.getText().toString();
        if(passowrd.length() == 0){
            ActivityHelper.showLongToast(mActivity,mActivity.getString(R.string.zs4game_password_no_empty));
            return;
        }
        httpPostLoginByPwd(account,passowrd);
    }

    private void accountRegist(){
        String account = etRegistAccount.getText().toString();
        if(account.length() == 0){
            ActivityHelper.showLongToast(mActivity,mActivity.getString(R.string.zs4Game_account_no_empty));
            return;
        }
        String passowrd = etRegistPassword.getText().toString();
        if(passowrd.length() == 0){
            ActivityHelper.showLongToast(mActivity,mActivity.getString(R.string.zs4game_password_no_empty));
            return;
        }
        if(account.length() < 3 || account.length() > 15){
            ActivityHelper.showLongToast(mActivity,mActivity.getString(R.string.zs4Game_account_limit));
            return;
        }
        if(passowrd.length() < 6 || passowrd.length() > 12){
            ActivityHelper.showLongToast(mActivity,mActivity.getString(R.string.zs4Game_password_limit));
            return;
        }
        httpPosRegist(account,passowrd);
    }
    protected  void initView(){
        if(mIsInit){
            return;
        }
        mIsInit = true;
        stuAccountRegist = (ViewStub)mMainView.findViewById(R.id.stu_login_regist);
        stuAccountLogin = (ViewStub)mMainView.findViewById(R.id.stu_login_account_login);
        stuAccountLogin.setVisibility(View.VISIBLE);
        tvTip = (TextView)mMainView.findViewById(R.id.zs4game_textview_retrivepassword_phone);
        tvSwitch = (TextView)mMainView.findViewById(R.id.tv_goto_regist);
        btnLogin = (Button)mMainView.findViewById(R.id.zs4game_btn_regist_and_login);
        btnLogin.setOnClickListener(this);
        tvSwitch.setOnClickListener(this);
        etAccount = (EditText)mMainView.findViewById(R.id.zs4game_login_account);
        etPassword = (EditText)mMainView.findViewById(R.id.zs4game_mPasswordView_login_password);
        btnLogin.setText(loginText);

    }
    private void initAccountRegist(){
        if(isInitAccountRegist){
            return;
        }
        isInitAccountRegist = true;
        etRegistAccount = (EditText)mMainView.findViewById(R.id.zs4game_regist_account);
        etRegistPassword = (EditText)mMainView.findViewById(R.id.zs4game_mPasswordView_regist_password);
    }
    @Override
    protected void show() {
        stuOtherAccountLogin.setVisibility(View.VISIBLE);
    }

    @Override
    protected void hide() {
        stuOtherAccountLogin.setVisibility(View.GONE);
    }

    @Override
    public void doOnClick(View v) {
        int id = v.getId();
        if(id == R.id.zs4game_btn_regist_and_login){
            login();
        }else if(id == R.id.tv_goto_regist){
            if(loginStyle == LOGIN){
                loginStyle = REGIST;
                stuAccountLogin.setVisibility(View.GONE);
                stuAccountRegist.setVisibility(View.VISIBLE);
                initAccountRegist();
                tvSwitch.setText(goToLoginText);
                btnLogin.setText(loginAndRegistText);

            }else{
                loginStyle = LOGIN;
                stuAccountLogin.setVisibility(View.VISIBLE);
                stuAccountRegist.setVisibility(View.GONE);
                tvSwitch.setText(goToRegistText);
                btnLogin.setText(loginText);
            }
        }
    }

    /**
     * 请求 帐号密码登录(113)
     *
     * @param account
     * @param password
     */
    private void httpPostLoginByPwd(final String account, final String password) {
        Log.e("login"," login account is "+account+" password is "+password);
        HandlerUtil.sendMessage(mLoginHandler, Extra.MessageKey.SHOWPROCESS);
        ArrayList<NameValuePair> paramList = new ArrayList<NameValuePair>();
        paramList.add(new BasicNameValuePair("account", EncryptHelper.encryptByAES(account)));
        paramList.add(new BasicNameValuePair("password", EncryptHelper.encryptByAES(EncryptHelper.md5(password))));
        new EventHandler2Get(
                Extra.URL.USER_LOGIN_ACCOUNT,
                paramList,
                ParamsHelper.getUserAgent1(),
                new CommonEventHandler.ResultHandlerInEvent() {

                    @Override
                    public void doResultHanler(JSONObject result) throws JSONException {

                        Log.e("login"," login json result is "+result.toString());
                        HandlerUtil.sendMessage(mLoginHandler, Extra.MessageKey.DISMISSPROCESS);

                        Message msg = mLoginHandler.obtainMessage();

                        msg.what = Extra.MessageKey.REQUEST_SUCCESS;
                        msg.obj = result;

                        Bundle bundle = new Bundle();
                        bundle.putString("param1", LOGINTYPE_EXCHANGE);
                        bundle.putString("param2", account);
                        bundle.putString("param3", password);

                        msg.setData(bundle);

                        // 发送消息
                        mLoginHandler.sendMessage(msg);
                    }

                    @Override
                    public void doExceptionHandler() {
                        HandlerUtil.sendMessage(mLoginHandler, Extra.MessageKey.DISMISSPROCESS);
                    }
                });
    }

    /**
     * 请求 帐号密码登录(113)
     *
     * @param account
     * @param password
     */
    private void httpPosRegist(final String account, final String password) {

        HandlerUtil.sendMessage(mLoginHandler, Extra.MessageKey.SHOWPROCESS);
        ArrayList<NameValuePair> paramList = new ArrayList<NameValuePair>();
        paramList.add(new BasicNameValuePair("account", EncryptHelper.encryptByAES(account)));
        paramList.add(new BasicNameValuePair("password", EncryptHelper.encryptByAES(EncryptHelper.md5(password))));
        new EventHandler2Get(
                Extra.URL.GUESTREGISTERURL,
                paramList,
                ParamsHelper.getUserAgent1(),
                new CommonEventHandler.ResultHandlerInEvent() {

                    @Override
                    public void doResultHanler(JSONObject result) throws JSONException {
                        HandlerUtil.sendMessage(mLoginHandler, Extra.MessageKey.DISMISSPROCESS);
                        Log.e("login"," regist json result is "+result.toString());
                        HandlerUtil.sendMessage(mLoginHandler, Extra.MessageKey.DISMISSPROCESS);

                        Message msg = mLoginHandler.obtainMessage();

                        msg.what = Extra.MessageKey.REQUEST_SUCCESS;
                        msg.obj = result;

                        Bundle bundle = new Bundle();
                        bundle.putString("param1", LOGINTYPE_EXCHANGE);
                        bundle.putString("param2", account);
                        bundle.putString("param3", password);

                        msg.setData(bundle);

                        // 发送消息
                        mLoginHandler.sendMessage(msg);
                    }

                    @Override
                    public void doExceptionHandler() {
                        HandlerUtil.sendMessage(mLoginHandler, Extra.MessageKey.DISMISSPROCESS);
                    }
                });
    }
    private class MyZs4GameTipDialog extends Zs4GameTipDialog {

        public MyZs4GameTipDialog(@NonNull Context context) {
            super(context);
        }
        public void startGame(){
          //  doRequestRegisterGuest();
            accountRegist();
            closeDialog();
        }
    }
}
